package com.atguigu.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MQConsumerConfig {
    /*
        创建延迟队列+延迟交换机+绑定
     */
    @Bean
    public TopicExchange delayExchange(){
        return ExchangeBuilder.topicExchange("test2_delay_exchange")
                .durable(true).ignoreDeclarationExceptions().build();
    }
    @Bean
    public Queue delayQueue(){
        return QueueBuilder.durable("test2_delay_queue")
                    .build();
    }
    @Bean
    public Binding delayBinding(Queue delayQueue,TopicExchange delayExchange){
        return BindingBuilder.bind(delayQueue).to(delayExchange).with("test2.delay");
    }
    /*
        创建死信队列+死信交换机+绑定(routingkey)
     */
    @Bean
    public TopicExchange deadExchange(){
        return ExchangeBuilder.topicExchange("test2_dead_exchange") //交换机类型+名称
                            .durable(true)//持久化
                            .ignoreDeclarationExceptions()//重复声明
                            .build();//构建
    }

    @Bean
    public Queue deadQueue(){
        return QueueBuilder.durable("test2_dead_queue")//持久化+队列名称
                            .ttl(20000)// 配置死信队列中消息的过期时间  单位毫秒
                            .deadLetterExchange("test2_delay_exchange")
                            .deadLetterRoutingKey("test2.delay")
                            .build();
    }

    @Bean
    public Binding deadBinding(TopicExchange deadExchange , Queue deadQueue ){
        return BindingBuilder.bind(deadQueue)//队列
                            .to(deadExchange) //要绑定的交换机
                            .with("test2.dead");//路由key
    }

    /*
        创建业务队列(绑定死信交换机+死信消息路由key)+业务交换机+绑定
               业务队列的消息消费时可能有临时的异常，可以将暂时不能正确消费的消息丢弃到死信队列中存储
     */

    @Bean
    public TopicExchange serviceExchange(){
        return ExchangeBuilder.topicExchange("test2_service_exchange")
                .durable(true)
                .ignoreDeclarationExceptions()
                .build();
    }
    @Bean
    public Queue serviceQueue(){
        return QueueBuilder.durable("test2_service_queue")
                            //配置死信交换机名称
                            .deadLetterExchange("test2_dead_exchange")
                            .deadLetterRoutingKey("test2.dead")//死信消息的路由key
                            .build();
    }

    @Bean
    public Binding serviceBinding(TopicExchange serviceExchange ,Queue serviceQueue ){
        return BindingBuilder.bind(serviceQueue)
                            .to(serviceExchange)
                            .with("test2.#");
    }
}
